package server;

import control.CControl;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class SWindowFrame extends JFrame
{
    private CControl cControl;
    
    // Formula panel variables
    private JTextArea formulaLogTextArea;
    private JScrollPane formulaLogScrollPane;
    private SFormulaConnectionPanel formulaConnectionPanel;
    
    // Server panel variables
    private JTextArea serverLogTextArea;
    private JScrollPane serverLogScrollPane;
    private SServerConnectionPanel serverConnectionPanel;
    
    
    public SWindowFrame()
    {
        super("SDU LiveView Web Server");
        
        cControl = new CControl();
        
        initComponent();
        setVisible(true);
        
        cControl.startStreaming();
        cControl.startServer(Integer.parseInt(cControl.getSettingsParameter("serverport")));
//        
//        addWindowListener(new WindowAdapter()
//        {
//            @Override
//            public void windowClosing(WindowEvent e)
//            {
//                cControl.stopServer();
//                try
//                {
//                    Thread.sleep(5000);
//                }
//                catch (InterruptedException ex) { }
//                System.exit(0);
//            }
//        });
    }
    
    private void initComponent()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 130);
        setLayout(new GridBagLayout());
        
        prepareFormulaPanel();
        prepareServerPanel();
        
        // Add panels to the window
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.weightx = 1.0;
        c.insets = new Insets(12, 12, 12, 6);
        c.fill = GridBagConstraints.BOTH;
        
        add(formulaConnectionPanel, c);
        c.gridx = 1;
        c.insets = new Insets(12, 6, 12, 12);
        add(serverConnectionPanel, c);
        
//        c.gridx = 0;
//        c.gridy = 1;
//        c.insets = new Insets(0, 12, 12, 6);
//        c.weighty = 1.0;
//        add(formulaLogScrollPane, c);
//        c.gridx = 1;
//        c.insets = new Insets(0, 6, 12, 12);
//        add(serverLogScrollPane, c);
    }
    
    public void setFormulaConnectionStatus(boolean isConnected)
    {
        if (isConnected)
        {
            formulaConnectionPanel.setBackground(new Color(0x00FF00));
        }
        else
        {
            formulaConnectionPanel.setBackground(new Color(0xFF0000));
        }
    }
    
    private void prepareFormulaPanel()
    {
        formulaLogScrollPane = new JScrollPane();
        formulaLogTextArea = new JTextArea();
        formulaLogTextArea.setLineWrap(true);
        formulaLogScrollPane.setViewportView(formulaLogTextArea);
        formulaLogScrollPane.setAutoscrolls(true);
        
        formulaConnectionPanel = new SFormulaConnectionPanel(cControl);
    }
    
    private void prepareServerPanel()
    {
        serverLogScrollPane = new JScrollPane();
        serverLogTextArea = new JTextArea();
        serverLogTextArea.setLineWrap(true);
        serverLogScrollPane.setViewportView(serverLogTextArea);
        serverLogScrollPane.setAutoscrolls(true);
        
        serverConnectionPanel = new SServerConnectionPanel(cControl);
    }

//    @Override
//    public void actionPerformed(ActionEvent e)
//    {
//
//       if( source == serverConnectionStartButton)
//       {
//           if (serverConnectionStartButton.getText().equals("Start"))
//           {
//               serverConnectionStartButton.setText("Stop");
//           }
//           else
//           {
//                serverConnectionStartButton.setText("Start");
//            }
//
//        }
//    }
}
